<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="text" id="username" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                <tip>填写自己管理账号的名称。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" id="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">真实姓名</label>
            <div class="layui-input-block">
                <input type="text" id="realName" name="realName" placeholder="请输入真实姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">手机号</label>
            <div class="layui-input-block">
                <input type="number" id="phone" name="phone" lay-verify="required" lay-reqtext="手机号不能为空" placeholder="请输入手机号" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let user = {
                "username" : $('#username').val(),
                "password" : $('#password').val(),
                "realName" : $('#realName').val(),
                "phone" : $('#phone').val()
            }
            console.log(user)
                $.ajax({
                    type: 'POST',
                    url: 'api/sport_manager/user/addUser',
                    data: JSON.stringify(user),
                    contentType: 'application/json',
                    success: function (data) {
                        if(data.msg === 'success'){
                            // 关闭弹出层
                            layer.close(parentIndex);
                            // 数据表格重载
                            table.reload('currentTableId', {
                                page: {
                                    curr: 1
                                }
                            }, 'data');
                        }else{
                            layer.msg('添加失败,用户名重复,请校验后再次添加',{time:1000},function(){
                                document.getElementById("username").value = '';
                                document.getElementById("password").value = '';
                            })
                        }
                    }
                })
                // 关闭弹出层
                // layer.close(index);
                // layer.close(parentIndex);

            ;


            return false;
        });

    });
</script>